import axios from 'axios'
const baseUrl = process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API : window.location.href.slice(0, window.location.href.indexOf('/site'))

let userAgent
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {
  userAgent = true
}else{
  userAgent=false
}
/**
   * 通用导出 .xlsx文件
   * @param {array} params 请求数据
   */
  export async function exportAll(url,params,name) {
    return await axios.get(baseUrl+url,{
      params: params,
      responseType:'blob',
    }).then(res=>{
      const link = document.createElement('a') 
      let blob = new Blob([res.data],{type:res.data.type})
      link.style.display = 'none'
      link.href=URL.createObjectURL(blob)
      link.download = name
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    })
  }

  export function download(url,name) {
    const link = document.createElement('a') 
    // let blob = new Blob([res.data],{type:res.data.type})
    link.style.display = 'none'
    link.href=url
    link.download = name
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }


  // 通用url地址下载
export function downloadAll(url,name) {
  const link = document.createElement('a') 
  let fileName =  name
  link.style.display = 'none'
  if(userAgent){
    link.href= url
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }else{
    link.href= url
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    // fetch(url,{mode:'no-cors'}).then(res => {
    //   return res.blob()
    // }).then(blob=>{
    //   link.href= URL.createObjectURL(blob)
    //   link.download = fileName
    //   document.body.appendChild(link)
    //   link.click()
    //   document.body.removeChild(link)
    // })
  }
}